{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using Voilà with the C++ kernel and interactive widgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <string>\n",
    "\n",
    "#include \"xwidgets/xoutput.hpp\"\n",
    "\n",
    "#include \"xleaflet/xmap.hpp\"\n",
    "#include \"xleaflet/xdraw_control.hpp\"\n",
    "#include \"xleaflet/xbasemaps.hpp\"\n",
    "\n",
    "namespace nl = nlohmann;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Create map widget\n",
    "auto water_color = xlf::basemap({\"OpenStreetMap\", \"France\"});\n",
    "\n",
    "auto map = xlf::map::initialize()\n",
    "    .layers({water_color})\n",
    "    .center({47, 363})\n",
    "    .zoom(5)\n",
    "    .finalize();\n",
    "\n",
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Create output widget to log draw events\n",
    "xw::output out;\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Options for the draw control\n",
    "nl::json polyline_options = {\n",
    "    {\"shapeOptions\", {\n",
    "        {\"color\", \"#6bc2e5\"},\n",
    "        {\"weight\", 8},\n",
    "        {\"opacity\", 1.0}\n",
    "    }}\n",
    "};\n",
    "\n",
    "nl::json polygon_options = {\n",
    "    {\"shapeOptions\", {\n",
    "        {\"fillColor\", \"#6be5c3\"},\n",
    "        {\"color\", \"#6be5c3\"},\n",
    "        {\"fillOpacity\", 1.0}\n",
    "    }},\n",
    "    {\"drawError\", {\n",
    "        {\"color\", \"#dd253b\"},\n",
    "        {\"message\", \"Oups!\"}\n",
    "    }},\n",
    "    {\"allowIntersection\", false}\n",
    "};\n",
    "\n",
    "nl::json circle_options = {\n",
    "    {\"shapeOptions\", {\n",
    "        {\"fillColor\", \"#efed69\"},\n",
    "        {\"fillOpacity\", 1.0},\n",
    "        {\"color\", \"#efed69\"}\n",
    "    }}\n",
    "};\n",
    "\n",
    "nl::json rectangle_options = {\n",
    "    {\"shapeOptions\", {\n",
    "        {\"fillColor\", \"#fca45d\"},\n",
    "        {\"fillOpacity\", 1.0},\n",
    "        {\"color\", \"#fca45d\"}\n",
    "    }}\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Log last action\n",
    "void print_draw_event(std::string action, nl::json geo_json)\n",
    "{\n",
    "    // Capturing the stdout with the output widget \n",
    "    auto guard = out.guard();\n",
    "    std::cout << action << \" a \" \n",
    "        << geo_json[\"geometry\"][\"type\"]\n",
    "        << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Add the draw control and event logger\n",
    "auto draw_control = xlf::draw_control::initialize()\n",
    "    .polyline(polyline_options)\n",
    "    .polygon(polygon_options)\n",
    "    .circle(circle_options)\n",
    "    .rectangle(rectangle_options)\n",
    "    .finalize();\n",
    "\n",
    "draw_control.on_draw(print_draw_event);\n",
    "map.add_control(draw_control);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
